<?php
namespace common\models;


class GroupFinalist extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'group_finalist';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['member_id', 'commodity_id', 'group_detail_id', 'group_money', 'finalist_time', 'create_time'], 'required'],
            [['member_id', 'commodity_id', 'group_detail_id', 'is_order', 'create_time'], 'integer'],
            [['group_money'], 'number'],
            [['finalist_time'], 'safe'],
            [['member_name'], 'string', 'max' => 64],
            [['member_mobile'], 'string', 'max' => 13],
            [['member_avatar'], 'string', 'max' => 128],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '入围人员管理表ID',
            'member_id' => '入围人员ID',
            'commodity_id' => '活动表ID',
            'group_detail_id' => '拼团详细表ID',
            'group_money' => '拼团价格',
            'finalist_time' => '入围时间',
            'member_name' => '入围用户人员名称',
            'member_mobile' => '入围用户手机号码',
            'member_avatar' => '入围用户头像',
            'is_order' => '状态 1=>已签约 2=》未签约 3=》签约中',
            'create_time' => '创建时间',
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
	{
		$model  = self::find();
		$where  = isset($whereArr['where']) ? $whereArr['where'] : [];

		$whereAnd = isset($whereArr['and'])?$whereArr['and']:[];
		$models = self::queryFormart($model,$where,$params,$whereAnd);
		$model  = $models['model'];
		
		$data  = $model->asarray()->all();

		if(!$data) return array();

		//扩展信息
		if(!$extends) return $data;
		foreach ($data as $k=>$v) {
            foreach ($extends as $extend) {
                if ($extend=='ruwei'){
                    if (strtotime($v['finalist_time'])>=time()){
                        unset($data[$k]);
                    }
                }
            }

        }
		return $data;	
	}

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}

    public static function insertAll($idArr,$group_detail_id){

        $detail = GroupDetail::getOne(['group_detail_id'=>$group_detail_id]);
        $id_arr =[];
        if (!$idArr) return false;
        foreach ($idArr as $k=>$v){
            if (!$k || !$v){return false;};
            $id=explode(',',$v);

            foreach ($id as $ik=>$iv) {
                $data = self::getAll(['member_id'=>$iv,'commodity_id'=>$detail['commodity_id'],'group_detail_id'=>$group_detail_id]);

                if ($data){
                    $counts = count($data);
                    if ($counts>6){
                        continue;
                    }
                    $timeArr = array_column($data,'finalist_time');
                    $time = date('Y-m-d') . ' ' . $k.":00";

                    if (in_array($time,$timeArr)){
                        continue;
                    }

                }

                $member = Member::getOne(['member_id' => $iv], 'member_name,member_mobile,member_avatar');
                $data = array(
                    'member_id'         => intval($iv),
                    "commodity_id"      => $detail['commodity_id'],
                    "group_detail_id"   => $group_detail_id,
                    "group_money"       => $detail['group_money'],
                    "finalist_time"     => date('Y-m-d') . ' ' . $k,
                    "member_name"       => $member['member_name'],
                    "member_mobile"     => $member['member_mobile'],
                    "member_avatar"     => $member['member_avatar'],
                    "is_order"          => 2,
                    "create_time"       => time()
                );

                $model = new GroupFinalist();
                $result =$model->load($data, '') && $model->save();
                if ($result){
                    $id_arr[] = $iv;
                }
            }

        }

        return $id_arr;
    }

}